<?php
/**
 * Description of formation
 *
 * @author Gmax
 */
class modules_cvtheque_formation extends modele {
    public static $_table = 'formation';
    public $type = 'formation';
    
    private $_id = 0;
    private $_user_id = 0;
    private $_description = '';
    private $_date_debut = '0000-00-00';
    private $_date_fin = '0000-00-00';
    private $_niveau_formation_id = 0;
    private $_niveau = 0;
    private $_niveau_nom = '';
    
    function __construct($id=null){
        if($id!==null){
            $this->_id = $id;
            $this->_constructObject();
        }
    }
    
    private function _constructObject(){
        $sqlQuery = 'SELECT ';
        $sqlQuery .= 't.*, ';
        $sqlQuery .= 'nf.nom AS niveau_nom, ';
        $sqlQuery .= 'nf.niveau AS niveau ';
        $sqlQuery .= 'FROM '.self::getTable().' AS t ';
        $sqlQuery .= 'LEFT JOIN '.PWE::getPrefixBdd().'niveau_formation AS nf ON nf.id=t.niveau_formation_id ';
        $sqlQuery .= 'WHERE t.id='.$this->_id.' ';
        $sqlQuery .= 'LIMIT 1';
        
        $datas = $this->_getOneResult($sqlQuery);
        if($datas){
            $this->_id = $datas['id'];
            $this->_user_id = $datas['user_id'];
            $this->_description = $this->outputFormat($datas['description']);
            $this->_date_debut = $datas['date_debut'];
            $this->_date_fin = $datas['date_fin'];
            $this->_niveau = $datas['niveau'];
            $this->_niveau_nom = $datas['niveau_nom'];
            $this->_niveau_formation_id = $datas['niveau_formation_id'];
        }
    }
    
    public function setId($id){
      $this->_id = $id;
    }
    public function getId(){
        return $this->_id;
    }
    public function getUserId(){
        return $this->_user_id;
    }
    /**
     * renvoi le candidat lié à cette formation
     * @return \modules_cvtheque_candidat
     */
    public function getUser(){
        $retour = null;
        if($this->getUserId()>0){
            $retour = new modules_cvtheque_candidat($this->getUserId());
        }
        return $retour;
    }
    /**
     * renvoi la description de la formation
     * @param boolean $input
     * @return string
     */
    public function getDescription($input=false){
        if($input)return $this->inputFormat($this->_description);
        else return $this->outputFormat($this->_description);
    }
    /**
     * renvoi la date de debut de formation
     * @return date
     */
    public function getDateDebut(){
        return $this->_date_debut;
    }
    /**
     * renvoi la date de fin de formation
     * @return date
     */
    public function getDateFin(){
        return $this->_date_fin;
    }
    /**
     * renoi l'id de formation
     * @return int
     */
    public function getNiveauId(){
        return $this->_niveau_formation_id;
    }
    /**
     * renoi le niveau de formation obtenu
     * @return int
     */
    public function getNiveau(){
        return $this->_niveau;
    }
    /**
     * renvoi le nom du niveau de formation obtenu
     * @param boolean $input
     * @return string
     */
    public function getNiveauNom($input=false){
        if($input)return $this->inputFormat($this->_niveau_nom);
        else return $this->outputFormat($this->_niveau_nom);
    }
    /**
     * renvoi le tableau des propriétés
     * @param boolean $input
     * @return array
     */
    public function getDatas($input=false) {
        $data = array();
        $data['id'] = $this->getId();
        $data['user_id'] = $this->getUserId();
        $data['description'] = $this->getDescription($input);
        $data['date_debut'] = $this->getDateDebut();
        $data['date_fin'] = $this->getDateFin();
        $data['niveau'] = $this->getNiveau();
        $data['niveau_nom'] = $this->getNiveauNom($input);
        $data['_niveau_formation_id'] = $this->getNiveauId();
        $data['modele'] = 'formation';
        
        return $data;
    }
    public function setDatas($datas) {
        foreach($datas as $k=>$v){
            if(substr($k, 0, 1) !== '_') $k = '_'.$k;
            if($k != '_id' && property_exists('modules_cvtheque_formation',$k)){
                $this->$k = $v;
            }
        }        
        return true;
    }
    
    public function save(){ 
        $sqlQuery = 'INSERT INTO '.self::getTable().' ';
        $sqlQuery .= '(user_id,description,date_debut,date_fin,niveau_formation_id) ';
        $sqlQuery .= 'VALUES (';
        $sqlQuery .= $this->getUserId().',';
        $sqlQuery .= '"'.$this->getDescription(true).'",';
        $sqlQuery .= '"'.$this->getDateDebut().'",';
        $sqlQuery .= '"'.$this->getDateFin().'",';
        $sqlQuery .= $this->getNiveauId();
        $sqlQuery .= ')';
        
        $result = bdd_sql::doSql($sqlQuery,false);
        if($result['count']>0){
            $this->setId(bdd_sql::getLastInsertId());
            $retour['success'] = true;
            $retour['datas'] = $this->getDatas();
        }else{
            $retour['error'] = bdd_sql::$LASTERROR;
        }
        return $retour;
    }

    public function update(){
        $sqlQuery = 'UPDATE '.self::getTable().' SET ';
        $sqlQuery .= 'description="'.$this->getDescription(true).'", ';
        $sqlQuery .= 'date_debut="'.$this->getDateDebut().'", ';
        $sqlQuery .= 'date_fin="'.$this->getDateFin().'", ';
        $sqlQuery .= 'niveau_formation_id='.$this->getNiveauId().' ';
        $sqlQuery .= 'WHERE id='.$this->getId().' LIMIT 1';
        
        bdd_sql::doSql($sqlQuery,false);
        return array('success'=>true,'datas'=>$this->getDatas());
    }
    /**
     *
     * @return array
     */
    public function delete() {
        $sqlDelete = 'DELETE FROM '.self::getTable().' WHERE id='.$this->_id;
        bdd_sql::doSql($sqlDelete,false);        
        return array('success'=>true,'datas'=>array());
    }
    
    /* methodes statiques */
    /**
     * retourne le nom de la table prefixé
     * @return string
     */
    public static function getTable(){
        return PWE::getPrefixBdd().self::$_table;
    }
    
    /**
     * renvoi la liste des niveau des diplomes et formations
     * @return array [id,nom,niveau]
     */
    public static function getListeNiveau(){
        $sqlQuery = 'SELECT * FROM '.PWE::getPrefixBdd().'niveau_formation ORDER BY niveau';
        $result = bdd_sql::doSql($sqlQuery,true);
        $retour = array();
        if($result['count']>0){
            foreach($result['results'] as $data){
                $retour[] = $data;
            }
        }
        return $retour;
    }
    
    /**
     * créé un nouveau niveau de formation en base et retourne l'id créé
     * @param string $nom
     * @param int $niveau
     * @return int
     */
    public static function createNiveau($nom,$niveau=0){
        $sqlQuery = 'REPLACE INTO '.PWE::getPrefixBdd().'niveau_formation (nom,niveau) VALUES ("'.$nom.'",'.$niveau.')';
        bdd_sql::doSql($sqlQuery,false);
        return bdd_sql::getLastInsertId();
    }
}
